"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.野猪Handler = void 0;
const Config_1 = require("../../Config");
const Point_1 = require("../Point");
const Utils_1 = __importDefault(require("../Utils"));
class 野猪Handler {
    constructor(gameMgr, handlerMgr) {
        this.gameMgr = gameMgr;
        this.handlerMgr = handlerMgr;
    }
    gc() {
        this.gameMgr = null;
        this.handlerMgr = null;
    }
    /**
     * 野猪冲撞，地形错误
     */
    impact(data, player) {
        return __awaiter(this, void 0, void 0, function* () {
            this.handlerMgr.spendAction(player, Config_1.Config.COST_野猪_IMPACT);
            let startPoint = this.gameMgr.map_point.get(data.start);
            let endPoint = this.gameMgr.map_point.get(data.end);
            let big_start = startPoint.big;
            let list = this.getPointList(data.start, data.end);
            //1.地形不对，停下来
            //2.有大子，杀掉后停下来
            //3.移动一下，保证野猪能获取视野
            for (let point of list) {
                if ([Point_1.Terrain.SHAN, Point_1.Terrain.JING, Point_1.Terrain.SHUI].includes(point.terrain)) {
                    break;
                }
                endPoint = point;
                if (point.big != null) {
                    break;
                }
                startPoint.big = null;
                endPoint.big = big_start;
                big_start.point = endPoint;
                this.gameMgr.sendBroadcast(Utils_1.default.getObj("big_move", { "start": startPoint.id, "end": endPoint.id }));
                startPoint = endPoint;
                yield this.delay(200);
            }
            let big_end = endPoint.big;
            if (big_end != null) {
                //大子被攻击
                if (big_end.attacked(player)) {
                    //打过了，
                }
                else {
                    //攻击没打过，自己死亡
                    big_start.die(player, endPoint.id);
                    return;
                }
            }
            startPoint.big = null;
            endPoint.big = big_start;
            big_start.point = endPoint;
            this.gameMgr.sendBroadcast(Utils_1.default.getObj("big_move", { "start": startPoint.id, "end": endPoint.id }));
        });
    }
    /**
     * 一共六个方向，一直获取point,直到获取不到
     */
    getPointList(start, end) {
        let startPoint = this.gameMgr.map_point.get(start);
        let endPoint = this.gameMgr.map_point.get(end);
        let direction_x = endPoint.position.x - startPoint.position.x;
        let direction_y = endPoint.position.y - startPoint.position.y;
        let list = Array();
        let point = endPoint;
        while (point != null) {
            list.push(point);
            let x = point.position.x + direction_x;
            let y = point.position.y + direction_y;
            point = this.gameMgr.map_point.get(Utils_1.default.getPointId(x, y));
        }
        return list;
    }
    // 延迟一下
    delay(ms) {
        return new Promise(resolve => {
            setTimeout(resolve, ms);
        });
    }
}
exports.野猪Handler = 野猪Handler;
